package com.quanyan.club.web;

import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaAdd;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaAddObj;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaPage;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaUpdate;
import com.quanyan.club.entity.vo.resp.RespClubMultimedia;
import com.quanyan.club.service.ClubMultimediaService;
import com.quanyan.common.Plugin.PageObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by zhang on 16/6/25.
 */
@RestController
@RequestMapping("/api/club/multimedia")
public class ClubMultimediaController {

    @Autowired
    private ClubMultimediaService clubMultimediaService;

    /**
     * 俱乐部相册列表
     * @param reqClubMultimediaPage
     * @return
     */
    @RequestMapping("/list")
    public APIResponse<PageObj<List<RespClubMultimedia>>> list(@Valid @RequestBody ReqClubMultimediaPage reqClubMultimediaPage){
        PageObj<List<RespClubMultimedia>> pageObj = this.clubMultimediaService.list(reqClubMultimediaPage);
        return APIResponse.returnSuccess(pageObj);
    }

    /**
     * 更新俱乐部相册
     * @param reqClubMultimediaUpdate
     * @param request
     * @return
     */
    @RequestMapping("/update")
    public APIResponse update(@Valid @RequestBody ReqClubMultimediaUpdate reqClubMultimediaUpdate,HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        if (reqClubMultimediaUpdate.getList().isEmpty()) {
            return APIResponse.returnFail("请选择您要进行操作的图片");
        }
        boolean flag = this.clubMultimediaService.update(reqClubMultimediaUpdate,uid);
        if(flag){
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail("操作出错，请重新操作");
    }

    /**
     * 添加俱乐部相册
     * @param reqClubMultimediaAddObj
     * @return
     */
    @RequestMapping("/add")
    public APIResponse add(@Valid @RequestBody ReqClubMultimediaAddObj reqClubMultimediaAddObj,HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");
        boolean flag = this.clubMultimediaService.add(reqClubMultimediaAddObj,uid);
        if(flag){
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("上传失败,请重新上传！");
        }
    }
}
